package com.fastjpa;

import com.efeichong.generator.DataSource;
import com.efeichong.generator.GenConfiguration;
import com.efeichong.generator.GenConstant;
import com.efeichong.generator.Generator;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Sets;

import java.util.Map;
import java.util.Set;

/**
 * @author lxk
 * @date 2020/12/3
 * @description 代码生成
 */
public class Generate {
    public static void main(String[] args) {
        generator();
    }

    static void generator() {
        Set<String> tables = Sets.newLinkedHashSet();
        tables.add("clazz");
        tables.add("student");
        tables.add("teacher");
        DataSource dataSource = new DataSource();
        dataSource.setDriver("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/fast-jpa?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&allowMultiQueries=true&useSSL=false");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        //自定义配置
        GenConfiguration configuration = new GenConfiguration();
        //po统一父类
//        configuration.setBaseEntity(BaseEntity.class);
        configuration.setDomainPackage("com.fastjpa.entity");
        configuration.setDaoPackage("com.fastjpa.dao");
        configuration.setServicePackage("com.fastjpa.service");
        configuration.setServiceImplPackage("com.fastjpa.service.impl");
        configuration.setControllerPackage("com.fastjpa.controller");
        configuration.setVoPackage("com.fastjpa.vo");
        configuration.setResponseClazz(ResponseData.class);
        //指定多对多中间表的维护者, 班级和老师多对多关系, 指定了关系表的数据是在对班级对象中的老师字段设置值并新增班级时生成
        Map map = ImmutableMap.of("clazz_teacher_ship", "teacher");
        Generator generator = Generator.builder().addGenConfiguration(configuration)
                .addRequireTableNames(tables)
//                .addHasExcel(true)
                .addDataSource(dataSource)
                .addGenerateModel(GenConstant.ALL)
                .addHasSwagger(false)
                .addManyToManyMappedTableMap(map)
                .build();
        generator.generate();
    }

}
